package com.byb.server.service.impl;

import com.byb.server.service.WcQuestionService;
import com.byb.server.domain.WcQuestion;
import com.byb.server.repository.WcQuestionRepository;
import com.byb.server.repository.search.WcQuestionSearchRepository;
import com.byb.server.service.dto.WcQuestionDTO;
import com.byb.server.service.mapper.WcQuestionMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing WcQuestion.
 */
@Service
@Transactional
public class WcQuestionServiceImpl implements WcQuestionService{

    private final Logger log = LoggerFactory.getLogger(WcQuestionServiceImpl.class);

    private final WcQuestionRepository wcQuestionRepository;

    private final WcQuestionMapper wcQuestionMapper;

    private final WcQuestionSearchRepository wcQuestionSearchRepository;
    public WcQuestionServiceImpl(WcQuestionRepository wcQuestionRepository, WcQuestionMapper wcQuestionMapper, WcQuestionSearchRepository wcQuestionSearchRepository) {
        this.wcQuestionRepository = wcQuestionRepository;
        this.wcQuestionMapper = wcQuestionMapper;
        this.wcQuestionSearchRepository = wcQuestionSearchRepository;
    }

    /**
     * Save a wcQuestion.
     *
     * @param wcQuestionDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public WcQuestionDTO save(WcQuestionDTO wcQuestionDTO) {
        log.debug("Request to save WcQuestion : {}", wcQuestionDTO);
        WcQuestion wcQuestion = wcQuestionMapper.toEntity(wcQuestionDTO);
        wcQuestion = wcQuestionRepository.save(wcQuestion);
        WcQuestionDTO result = wcQuestionMapper.toDto(wcQuestion);
        //wcQuestionSearchRepository.save(wcQuestion);
        return result;
    }

    /**
     *  Get all the wcQuestions.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<WcQuestionDTO> findAll(Pageable pageable) {
        log.debug("Request to get all WcQuestions");
        return wcQuestionRepository.findAll(pageable)
            .map(wcQuestionMapper::toDto);
    }

    /**
     *  Get one wcQuestion by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public WcQuestionDTO findOne(Long id) {
        log.debug("Request to get WcQuestion : {}", id);
        WcQuestion wcQuestion = wcQuestionRepository.findOne(id);
        return wcQuestionMapper.toDto(wcQuestion);
    }

    /**
     *  Delete the  wcQuestion by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete WcQuestion : {}", id);
        wcQuestionRepository.delete(id);
       // wcQuestionSearchRepository.delete(id);
    }

    /**
     * Search for the wcQuestion corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<WcQuestionDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of WcQuestions for query {}", query);
        Page<WcQuestion> result = wcQuestionSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(wcQuestionMapper::toDto);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<WcQuestionDTO> getWcQuestionByProductId(Long id,Pageable pageable) {
        return  wcQuestionRepository.findAllByWcProductId(id,pageable).map(wcQuestionMapper::toDto);
    }
}
